path = "/var/www/cgi-bin/r/modules/calc_two_min_variance_portfolios.r"

print( path )
source( path )


# 正常系のテスト
test_calc_two_min_variance_portfolios_000 <- function()
{
	print( "test case 000" )
	means <- c( 0.151, 0.125, 0.147, 0.0902, 0.1768 )
	covariances <- 
		matrix(
			c(  0.023,  0.0093,  0.0062,  0.0074, -0.0023,
			        0,  0.0140,  0.0022,  0.0056,  0.0026,
			        0,       0,  0.0180,  0.0078, -0.0027,
					0,       0,       0,  0.0340, -0.0056,
					0,       0,       0,       0,  0.0260 ),
			nrow = 5, ncol = 5 )
	covariances <- covariances + t( covariances )
	diag( covariances ) <- diag( covariances ) / 2
	res <- calc_two_min_variance_portfolios( means, covariances, TRUE )	
#	print( res )
	if ( is.null( res$error_message ) == FALSE )
	{
		cat( "expect: res$error_message == NULL, res$error_message: ", res$error_message, "\n" )
	}

	digit <- 9
	calc_error <- 0.0000001
	deltas <- round( res$ratio1, digit ) - round( c( 0.08825144, 0.25086041, 0.28238423, 0.10375097, 0.27475295 ), digit )
	deltas <- abs( deltas )
	if ( length( deltas[ deltas > calc_error ] ) != 0 )
	{
		cat( "expect: length( deltas[ deltas > calc_error ] ) == 0, res$ratio1: ", res$ratio1, "\n" )
	}

	deltas <- round( res$ratio2, digit ) - round( c( 0.1583499, 0.1553502, 0.3142633, 0.0379156, 0.3341211 ), digit )
	deltas <- abs( deltas )
	if ( length( deltas[ deltas > calc_error ] ) != 0 )
	{
		cat( "expect: length( deltas[ deltas > calc_error ] ) == 0, res$ratio2: ", res$ratio2, "\n" )
		print( deltas )
	}
#	print( deltas )
}

# 異常系のテスト
test_calc_two_min_variance_portfolios_001 <- function()
{
	print( "test case 001" )
	means <- c( 1, 2, 3 )
	covariances <- matrix( c( 1, 2, 3, 4 ), nrow = 2, ncol = 2 )

	res <- calc_two_min_variance_portfolios( means, covariances, TRUE )	
	if ( is.null( res$error_message ) )
	{
		cat( "res$error_message != NULL, NULL", "\n" )
	}
}

# run test
test_calc_two_min_variance_portfolios_000()
test_calc_two_min_variance_portfolios_001()